package com.ruoyi.bantong.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ruoyi.bantong.constant.BanTongConstants;
import com.ruoyi.bantong.domain.BtPlacecardConfig;
import com.ruoyi.bantong.domain.BtUserLevel;
import com.ruoyi.bantong.domain.BtUserTicket;
import com.ruoyi.bantong.mapper.*;
import com.ruoyi.bantong.service.IBtUserAccountRecordService;
import com.ruoyi.common.core.domain.entity.BtUser;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bantong.domain.BtPlacecardApply;
import com.ruoyi.bantong.service.IBtPlacecardApplyService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 版通申请Service业务层处理
 * 
 * @author laohei
 * @date 2021-01-26
 */
@Service
public class BtPlacecardApplyServiceImpl implements IBtPlacecardApplyService 
{
    @Autowired
    private BtPlacecardApplyMapper btPlacecardApplyMapper;

    @Autowired
    private BtPlacecardConfigMapper configMapper;
    @Autowired
    private BtUserMapper btUserMapper;

    @Autowired
    private BtUserLevelMapper btUserLevelMapper;
    @Autowired
    private IBtUserAccountRecordService userAccountRecordService;
    @Autowired
    private BtUserTicketMapper ticketMapper;

    /**
     * 查询版通申请
     * 
     * @param paId 版通申请ID
     * @return 版通申请
     */
    @Override
    public BtPlacecardApply selectBtPlacecardApplyById(Integer paId)
    {
        return btPlacecardApplyMapper.selectBtPlacecardApplyById(paId);
    }

    /**
     * 查询版通申请列表
     * 
     * @param btPlacecardApply 版通申请
     * @return 版通申请
     */
    @Override
    public List<BtPlacecardApply> selectBtPlacecardApplyList(BtPlacecardApply btPlacecardApply)
    {
        return btPlacecardApplyMapper.selectBtPlacecardApplyList(btPlacecardApply);
    }

    @Override
    public List<Map<String,Object>> selectBtPlacecardApplyListByStatus() {
        return btPlacecardApplyMapper.selectBtPlacecardApplyListByStatus();
    }

    /**
     * 新增版通申请
     * 
     * @param btPlacecardApply 版通申请
     * @return 结果
     */
    @Override
    public int insertBtPlacecardApply(BtPlacecardApply btPlacecardApply)
    {
        btPlacecardApply = btPlacecardApplyMapper.selectBtPlacecardApplyByPpId(btPlacecardApply.getPpId(), btPlacecardApply.getUserId());
        if(StringUtils.isNull(btPlacecardApply)){
            throw new RuntimeException("未查询到中奖记录");
        }
        btPlacecardApply.setUpdateTime(DateUtils.getNowDate());
        btPlacecardApply.setUpdateBy(btPlacecardApply.getUserId().toString());
        return btPlacecardApplyMapper.updateBtPlacecardApply(btPlacecardApply);
    }

    /**
     * 修改版通申请
     * 
     * @param btPlacecardApply 版通申请
     * @return 结果
     */
    @Override
    @Transactional
    public int updateBtPlacecardApply(BtPlacecardApply btPlacecardApply) throws Exception
    {
        BtPlacecardApply bapply = btPlacecardApplyMapper.selectBtPlacecardApplyById(btPlacecardApply.getPaId());
        if(1 == btPlacecardApply.getStatus()){
            Integer userId = bapply.getUserId();
            BtUser btUser = btUserMapper.selectBtUserById(userId);
            BigDecimal btBean = btUser.getBtBean();
            BigDecimal accountMoney = BigDecimal.valueOf(bapply.getConvertMoney()); //兑换金额
            BigDecimal earnestMoney = BigDecimal.valueOf(bapply.getEarnestMoney()); //保证金
            BigDecimal subtract = accountMoney.subtract(earnestMoney);
            if(subtract.compareTo(btBean) == 1){
                throw new CustomException("用户余额不足");
            }
            //扣出余额
            userAccountRecordService.updataUserAccount(userId,subtract,BigDecimal.ZERO,
                    btPlacecardApply.getPaId(),"bt_placecard_apply","out",6);
            //增加版通
            userAccountRecordService.updataUserAccount(userId,BigDecimal.ZERO,accountMoney,
                    btPlacecardApply.getPaId(),"bt_placecard_apply","in",7);
            Integer level = bapply.getLevel();
            BtUserLevel btUserLevel = btUserLevelMapper.selectBtUserLevelByLevel(level);
            Integer winGotNum = btUserLevel.getWinGotNum(); //中奖后获得奖券数量
            if(winGotNum > 0){
                for (int i = 0; i < winGotNum; i++) {
                    //奖励奖券
                    BtUserTicket btUserTicket = new BtUserTicket();
                    btUserTicket.setUserId(userId);
                    btUserTicket.setTotalFee(1500);
                    btUserTicket.setCreateTime(DateUtils.getNowDate());
                    ticketMapper.insertBtUserTicket(btUserTicket);
                }
            }
        }
        if(0 == btPlacecardApply.getStatus()){ //提交审核
            btPlacecardApply.setApplyTime(DateUtils.getNowDate());
        }
        btPlacecardApply.setUpdateTime(DateUtils.getNowDate());
        btPlacecardApply.setUpdateBy(SecurityUtils.getUsername());
        return btPlacecardApplyMapper.updateBtPlacecardApply(btPlacecardApply);
    }

    /**
     * 批量删除版通申请
     * 
     * @param paIds 需要删除的版通申请ID
     * @return 结果
     */
    @Override
    public int deleteBtPlacecardApplyByIds(Integer[] paIds)
    {
        return btPlacecardApplyMapper.deleteBtPlacecardApplyByIds(paIds);
    }

    /**
     * 删除版通申请信息
     * 
     * @param paId 版通申请ID
     * @return 结果
     */
    @Override
    public int deleteBtPlacecardApplyById(Integer paId)
    {
        return btPlacecardApplyMapper.deleteBtPlacecardApplyById(paId);
    }
}
